package utils

import (
	"clouds/models"
)

type TreeList struct {
	Id              int         `json:"id"`
	Pid             int         `json:"pid"`
	RouterTitle     string      `json:"router_title"`
	RouterPath      string      `json:"router_path"`
	RouterName      string      `json:"router_name"`
	RouterComponent string      `json:"router_component"`
	Children        []*TreeList `json:"children"`
}

func getTree(menus []*models.Routers) []*TreeList {
	treeList := []*TreeList{}
	for _, value := range menus {
		node := &TreeList{
			Id: value.Id,
			Pid: value.Pid,
			RouterTitle: value.RouterTitle,
			RouterPath: value.RouterPath,
			RouterName: value.RouterName,
			RouterComponent: value.RouterComponent,
		}
		children := []*TreeList{}
		for _, value1 := range menus {
			if node.Id == value1.Pid {
				child := &TreeList{
					Id: value1.Id,
					Pid: value1.Pid,
					RouterTitle: value1.RouterTitle,
					RouterPath: value1.RouterPath,
					RouterName: value1.RouterName,
					RouterComponent: value1.RouterComponent,
				}
				children = append(children, child)
			}
		}
		node.Children = children
		treeList = append(treeList, node)
	}
	return treeList
}
